<template>
	<!-- 我的收藏 -->
	<view class="collect">
		<view class="collect-filter">
			<view class="filter" @click="changecurrent" :class="[!current ? 'active' : '']">商品({{ shopList.length }})</view>
			<view class="filter" @click="changecurrent" :class="[current ? 'active' : '']">视频({{ videoList.length }})</view>
		</view>

		<view class="collect-list" v-if="current == 0">
			<view v-if="shopList.length > 0">
				<view class="collect-item" v-for="(item, index) in shopList" :key="item.id">
					<view class="shopSidewise-main" @click="gotoShopInfo(item.goods_id, item.type)">
						<view class="shopSidewise-image">
							<image :src="imageUrls(item.goods.fileUrl)" mode="widthFix" class="shopImage"></image>
						</view>
						<view class="shopSidewise-info">
							<view class="shopSidewise-info-name">
								{{ item.goods.name }}
							</view>
							<view class="shopSidewise-info-des">规格：{{ item.goods.spec }}</view>
							<view class="shopSidewise-info-tags">
								<uni-tag :text="item.goods.shopName" type="error" size="mini" style="margin-right: 10rpx" />
								<uni-tag :text="item.goods.artid ? '云仓发货' : '商家自营'" type="error" size="mini" style="margin-left: 8rpx" :inverted="true" />
							</view>
							<view class="shopSidewise-info-pri">
								￥
								<text style="font-size: 32rpx">{{ getShopPrice(item.goods) }}</text>
							</view>
							<view class="shopSidewise-info-salesVolume">
								<view class="shop-salesvolume">
									月销
									<text style="padding: 0px 6rpx">{{ item.goods.total }}</text>
									件
								</view>
								<view class="shop-cart" @click.stop="addCard(item.goods_id)">
									<u-icon name="shopping-cart" size="18" color="#fff"></u-icon>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<u-empty mode="list" v-else icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
		</view>

		<view class="videoList" v-if="current == 1">
			<view v-if="videoList.length > 0">
				<hwxcSearchVideo :videoInfoData="videoList" />
			</view>

			<u-empty mode="list" v-else icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
		</view>
	</view>
</template>

<script>
import hwxcShopSidewise from '../../components/hwxcShopSidewise/hwxcShopSidewise.vue';
import hwxcSearchVideo from '../../components/hwxcSearchVideo/hwxcSearchVideo.vue';
import { userCollectList } from '../../api/home/index.js';
import { cloudAddCart } from '../../api/home/index.js';
import { videoCollectList } from '../../api/video/index.js';
import { imageUrl } from '../../utils/tools';
export default {
	data() {
		return {
			current: false,
			shopList: [],
			videoList: [],
			ratio: 0
		};
	},
	components: {
		hwxcShopSidewise,
		hwxcSearchVideo
	},
	async onLoad() {
		this.ratio = this.$store.state.commInitInfo.yyd_ratio;
		await this.getuserCollectList();
		await this.getvideoCollectList();
	},
	methods: {
		getShopPrice(item) {
			// 云店根据费率计算
			if (item.type == 0) {
				if (!item.store_id) {
					let pic = item.price != '0.00' ? item.price : item.saleprice;
					return (Number(pic) + (Number(pic) * this.ratio) / 100).toFixed(2);
				} else {
					return item.price != '0.00' ? item.price : item.saleprice;
				}
			} else {
				return item.price != '0.00' ? item.price : item.saleprice;
			}
		},

		// 获取收藏商品
		async getuserCollectList() {
			try {
				const resData = await userCollectList();
				this.shopList = resData.data;
			} catch (e) {
				throw Error(e);
			}
		},

		// 获取视频收藏列表
		async getvideoCollectList() {
			try {
				const resData = await videoCollectList();
				const videoList = resData.data;
				videoList.forEach((item) => {
					this.videoList.push(item.video);
				});
			} catch (e) {
				throw Error(e);
			}
		},

		gotoShopInfo(id) {
			uni.navigateTo({
				url: `/pages/shoppingInfo/shoppingInfo?id=${id}`
			});
		},

		// 图片路径拼接
		imageUrls(fileUrl) {
			return imageUrl(fileUrl);
		},

		// 加入购物车
		async addCard(id) {
			await this.getcloudAddCart(id);
		},

		//  加入购物车
		async getcloudAddCart(id) {
			try {
				const queryData = {
					id,
					num: 1
				};
				const resData = await cloudAddCart(queryData);
				uni.showToast({
					title: '添加成功',
					icon: 'none'
				});
			} catch (e) {
				throw Error(e);
			}
		},

		changecurrent() {
			this.current = !this.current;
		}
	}
};
</script>

<style>
page {
	background-color: #f3f3f3;
}
</style>

<style scoped lang="scss">
.collect {
	.collect-filter {
		background-color: #fff;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-bottom: 1px #f3f3f3 solid;
		.filter {
			flex: 1;
			text-align: center;
			color: #444;
			font-size: 28rpx;
		}
		.active {
			color: #e43927;
		}
	}
	.collect-list {
		box-sizing: border-box;
		padding: 20rpx;
		.collect-item {
			background-color: #fff;
			box-sizing: border-box;
			padding: 20rpx;
			margin-bottom: 30rpx;
			border-radius: 24rpx;
		}
	}
}

.shopSidewise-main {
	display: flex;
	.shopSidewise-image {
		width: 240rpx;
		.shopImage {
			width: 220rpx;
			height: 220rpx;
			background-color: #f3f3f3;
		}
	}

	.shopSidewise-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.shopSidewise-info-name {
			font-size: 27rpx;
			font-weight: bold;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
		}

		.shopSidewise-info-des {
			color: #999;
			font-size: 26rpx;
			margin-top: 8rpx;
		}

		.shopSidewise-info-tags {
			margin-bottom: 8rpx;
			margin: 10rpx 0px;
			display: flex;
			width: 100%;
		}

		.shopSidewise-info-pri {
			font-size: 24rpx;
			color: red;
			font-weight: bold;
		}

		.shopSidewise-info-salesVolume {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.shop-salesvolume {
				color: #999;
				font-size: 24rpx;
			}

			.shop-cart {
				height: 45rpx;
				width: 45rpx;
				border-radius: 50%;
				background-color: red;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
}

.videoList {
	box-sizing: border-box;
	padding: 20rpx;
}
</style>
